﻿using GBlog.Core;
using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class RouteCollectionExtensions
    {
        public static Route MapRoute(this RouteCollection routes, string name, string url, bool lowercase)
        {
            return routes.MapRoute(name, url, null, null, null, lowercase);
        }

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, bool lowercase)
        {
            return routes.MapRoute(name, url, defaults, null, null, lowercase);
        }

        public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces, bool lowercase)
        {
            return routes.MapRoute(name, url, null, null, namespaces, lowercase);
        }

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, bool lowercase)
        {
            return routes.MapRoute(name, url, defaults, constraints, null, lowercase);
        }

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces, bool lowercase)
        {
            return routes.MapRoute(name, url, defaults, null, namespaces, lowercase);
        }

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces, bool lowercase)
        {
            if (lowercase)
            {
                if (routes == null)
                {
                    throw new ArgumentNullException("routes");
                }

                if (url == null)
                {
                    throw new ArgumentNullException("url");
                }

                var route = new GRoute(url, new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(defaults),
                    Constraints = new RouteValueDictionary(constraints),
                    DataTokens = new RouteValueDictionary(namespaces),
                };
                if (namespaces != null && namespaces.Length > 0)
                {
                    route.DataTokens["Namespaces"] = namespaces;
                }
                routes.Add(name, route);
                return route;
            }
            return routes.MapRoute(name, url, defaults, constraints, namespaces);
        }
    }
}